package com.app.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.app.model.Group;
import com.app.model.Image;
import com.app.model.Purpose;
import com.app.model.User;

public class UtilGsonOld {

    // private static final Gson gson = getGson();
    //
    // private static Gson getGson() {
    // GsonBuilder gsonBuilder = new GsonBuilder();
    // gsonBuilder.registerTypeAdapter(Calendar.class, new
    // CalendarSerializer());
    // gsonBuilder.registerTypeAdapter(Calendar.class, new
    // CalendarDeserializer());
    // return gsonBuilder.create();
    // }

    public static User jsonToUser(String json) throws JSONException {
	User user = new User();
	JSONObject jObj = new JSONObject(json);

//	if (Util.isNotEmptyOrNullOrZero(jObj.getString("birthday")))
//	    user.setBirthday(Util.stringToCalendar(jObj.getString("birthday"), Util.FORMAT_DATE_JSON));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getString("email")))
	    user.setEmail(jObj.getString("email"));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getString("faceBookId")))
	    user.setFacebookId(jObj.getString("faceBookId"));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getString("gender")))
	    user.setGender(jObj.getString("gender"));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getLong("id")))
	    user.setId(jObj.getLong("id"));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getString("name")))
	    user.setName(jObj.getString("name"));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getString("pass")))
	    user.setPass(jObj.getString("pass"));

	return user;
    }

    public static List<User> jsonToListUser(JSONArray jsonArray) throws JSONException {
	List<User> listUser = new ArrayList<User>();
	for (int i = 0; i < jsonArray.length(); i++) {
	    listUser.add(jsonToUser(jsonArray.getJSONObject(i).toString()));
	}
	return listUser;
    }

    public static Image jsonToImage(String json) throws JSONException {
	Image image = new Image();
	JSONObject jObj = new JSONObject(json);
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getLong("id")))
	    image.setId(jObj.getLong("id"));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getString("url")))
	    image.setUrl(jObj.getString("url"));

	return image;
    }

    public static Purpose jsonToPurpose(String json) throws JSONException {
	Purpose purpose = new Purpose();
	JSONObject jObj = new JSONObject(json);

	if (Util.isNotEmptyOrNullOrZero(jObj.getString("id")))
	    purpose.setId(jObj.getInt("id"));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getString("url")))
	    purpose.setDescription(jObj.getString("url"));

	return purpose;
    }

    public static Group jsonToGroup(String json) throws JSONException {
	Group group = new Group();
	JSONObject jObj = new JSONObject(json);

	if (Util.isNotEmptyOrNullOrZero(jObj.getLong("id")))
	    group.setId(jObj.getLong("id"));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getInt("countUser")))
	    group.setCountUser(jObj.getInt("countUser"));
	
//	if (Util.isNotEmptyOrNullOrZero(jObj.getString("date")))
//	    group.setDate(Util.stringToCalendar(jObj.getString("date"), Util.FORMAT_DATE_JSON));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getString("destination")))
	    group.setDestination(jObj.getString("destination"));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getString("image")))
	    group.setImage(jsonToImage(jObj.getString("image")));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getString("owner")))
	    group.setOwner(jsonToUser(jObj.getString("owner")));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getString("purpose")))
	    group.setPurpose(jsonToPurpose(jObj.getString("purpose")));
	
	if (Util.isNotEmptyOrNullOrZero(jObj.getJSONArray("listUser")))
	    group.setUserList(jsonToListUser(jObj.getJSONArray("listUser")));

	return group;
    }

    public static String groupToJson(Group group) throws JSONException {
	JSONObject jsonObj = new JSONObject();

	if (Util.isNotEmptyOrNullOrZero(group.getCountUser()))
	    jsonObj.put("countUser", group.getCountUser());
	
	if (Util.isNotEmptyOrNullOrZero(group.getDestination()))
	    jsonObj.put("destination", group.getDestination());
	
//	if (Util.isNotEmptyOrNullOrZero(group.getDate()))
//	    jsonObj.put("date", Util.dateToString(group.getDate().getTime(), Util.FORMAT_DATE_JSON));
	
	if (Util.isNotEmptyOrNullOrZero(group.getId()))
	    jsonObj.put("id", group.getId());
	
	if (Util.isNotEmptyOrNullOrZero(group.getImage()))
	    jsonObj.put("image", imageToJson(group.getImage()));
	
	if (Util.isNotEmptyOrNullOrZero(group.getOwner()))
	    jsonObj.put("owner", userToJson(group.getOwner()));
	
	if (Util.isNotEmptyOrNullOrZero(group.getPurpose()))
	    jsonObj.put("purpose", purposeToJson(group.getPurpose()));
	
	if (Util.isNotEmptyOrNullOrZero(group.getUserList()))
	    jsonObj.put("userList", listUserToJson(group.getUserList()));

	return jsonObj.toString();
    }

    public static String userToJson(User user) throws JSONException {
	JSONObject jsonObj = new JSONObject();

//	if (Util.isNotEmptyOrNullOrZero(user.getBirthday()))
//	    jsonObj.put("birthday", Util.dateToString(user.getBirthday().getTime(), Util.FORMAT_DATE_JSON));

	if (Util.isNotEmptyOrNullOrZero(user.getEmail()))
	    jsonObj.put("email", user.getEmail());
	
	if (Util.isNotEmptyOrNullOrZero(user.getFacebookId()))
	    jsonObj.put("favebookId", user.getFacebookId());
	
	if (Util.isNotEmptyOrNullOrZero(user.getGender()))
	    jsonObj.put("gender", user.getGender());
	
	if (Util.isNotEmptyOrNullOrZero(user.getId()))
	    jsonObj.put("id", user.getId());
	
	if (Util.isNotEmptyOrNullOrZero(user.getName()))
	    jsonObj.put("name", user.getName());
	
	if (Util.isNotEmptyOrNullOrZero(user.getPass()))
	    jsonObj.put("pass", user.getPass());

	return jsonObj.toString();
    }

    public static String listUserToJson(Collection<User> collection) throws JSONException {
	JSONArray jsonArr = new JSONArray();
	for (User user : collection) {
	    jsonArr.put(userToJson(user));
	}
	return jsonArr.toString();
    }

    public static String imageToJson(Image image) throws JSONException {
	JSONObject jsonObj = new JSONObject();

	if (Util.isNotEmptyOrNullOrZero(image.getId()))
	    jsonObj.put("id", image.getId());
	
	if (Util.isNotEmptyOrNullOrZero(image.getUrl()))
	    jsonObj.put("url", image.getUrl());

	return jsonObj.toString();
    }

    public static String purposeToJson(Purpose purpose) throws JSONException {
	JSONObject jsonObj = new JSONObject();

	if (Util.isNotEmptyOrNullOrZero(purpose.getId()))
	    jsonObj.put("id", purpose.getId());
	
	if (Util.isNotEmptyOrNullOrZero(purpose.getDescription()))
	    jsonObj.put("description", purpose.getDescription());

	return jsonObj.toString();
    }

}
